// pages/Photos/Photos.js
const App = getApp()
const systemInfo = App.systemInfo
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 1,
        images: [],
        array: [],
        w: parseInt(systemInfo.windowWidth),
        h: parseInt(systemInfo.windowHeight) - 100,
        v750: 750 / parseInt(systemInfo.windowWidth)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options)
        let temp = options
        let array = []
        temp.images = temp.images.split(',')
        temp.images.map((item)=>{
            array.push({
                url: item,
                w: 0,
                h: 0,
                b: 0
            })
        })
        temp.array = array
        temp.index = parseInt(temp.index)
        this.setData(temp)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    changeSwiper(e){
        // console.log(e)
        this.setData({
            index: e.detail.current
        })
    },

    setImageSize(e){
        // console.log(e)
        // console.log(e.target.dataset.index)
        let temp = this.data.array
        let w = e.detail.width, h = e.detail.height, b = e.detail.height / e.detail.width, v750 = this.data.v750
        let tempW = 0, tempH = 0
        if(w/v750 > this.data.w){
            tempW = 750
            if(h/v750 > this.data.h){
                tempH = this.data.h * v750
                tempW = tempH / b
            }else{
                tempH = tempW * b
            }
            temp[e.target.dataset.index].w = tempW
            temp[e.target.dataset.index].h = tempH
            temp[e.target.dataset.index].b = b
        }
        else{
            tempW = w
            if (h / v750 > this.data.h) {
                tempH = this.data.h * v750
                tempW = tempH / b
            } else {
                tempH = tempW * b
            }
            temp[e.target.dataset.index].w = tempW
            temp[e.target.dataset.index].h = tempH
            temp[e.target.dataset.index].b = b
        }
        this.setData({
            array: {
                ...this.data.array,
                ...temp
            }
        })        
    }
})